package com.goldenautumn.prefabricate.activity.mine

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.goldenautumn.prefabricate.MainActivity
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.mine.adapter.UserMenuAdapter
import com.goldenautumn.prefabricate.activity.mine.address.AddressManageActivity
import com.goldenautumn.prefabricate.activity.mine.order.OrderListActivity
import com.goldenautumn.prefabricate.activity.splash.ProtocolActivity
import com.goldenautumn.prefabricate.business.entity.UserMenuVo
import com.goldenautumn.prefabricate.business.user.UserInfoUiState
import com.goldenautumn.prefabricate.business.user.UserViewModel
import com.goldenautumn.prefabricate.databinding.FragmentMineBinding
import com.goldenautumn.prefabricate.utils.Constants.PROTOCOL_PRIVACY
import com.goldenautumn.prefabricate.utils.isLogin
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.goldenautumn.prefabricate.utils.onLogout
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseFragment
import qncx.external.common.utils.intent
import qncx.external.common.widget.dialog.PromptDialog

class MineFragment : BaseFragment<FragmentMineBinding>() {

    private val mUserModel by viewModels<UserViewModel>()

    private val mMenuAdapter by lazy {
        UserMenuAdapter()
    }

    override fun getLayoutId(): Int = R.layout.fragment_mine

    override suspend fun initView() {

        mBinding.menuRv.layoutManager = LinearLayoutManager(mContext)
        mBinding.menuRv.adapter = mMenuAdapter

        mMenuAdapter.submitList(arrayListOf(
            UserMenuVo(name = getString(R.string.user_menu_orders), img = R.mipmap.ic_mine_function_0,type = "0"),
            UserMenuVo(name = getString(R.string.user_menu_account), img = R.mipmap.ic_mine_function_1,type = "1"),
            UserMenuVo(name = getString(R.string.user_menu_language), img = R.mipmap.ic_mine_function_2,type = "2"),
            UserMenuVo(name = getString(R.string.mine_menu_address), img = R.mipmap.ic_mine_function_3,type = "3"),
            UserMenuVo(name = getString(R.string.user_menu_notice), img = R.mipmap.ic_mine_function_5,type = "5"),
            UserMenuVo(name = getString(R.string.user_menu_client), img = R.mipmap.ic_mine_function_4,type = "4"),
            UserMenuVo(name = getString(R.string.user_logout), img = R.mipmap.ic_mine_function_6,type = "6"),
        ))

        mMenuAdapter.setOnItemClickListener {adapter,_,position ->
            var item = adapter.getItem(position)
            when(item?.type) {
                "0" -> {
                    OrderListActivity.start(mContext,0)
                }
                "1" -> {
                    startActivity(UserInfoActivity::class.intent)
                }
                "2" -> {
                    startActivity(LanguageActivity::class.intent)
                }
                "3" -> {
                    startActivity(AddressManageActivity::class.intent)
                }
                "4" -> {
                    var arr = resources.getStringArray(R.array.user_agreement)
                    startActivity(ProtocolActivity::class.intent.putExtra(PROTOCOL_PRIVACY, arr[1]))
                }
                "5" -> {
                    startActivity(NoticeActivity::class.intent)
                }
                "6" -> {
                    PromptDialog().setConfirmListener(object : PromptDialog.ConfirmHandleListener {
                        override fun bindView(dialog: PromptDialog, view: PromptDialog.PromptModel) {
                            view.setTitleText(getString(R.string.user_sign_out))
                            view.setMsgText(getString(R.string.user_sign_out_msg))
                            view.setShowCancel(true)
                            view.getPromptConfirm().setOnClickListener {
                                dialog.dismiss()
                                onLogout()
                                (activity as  MainActivity).goHome(0)
                                onCenterToast(getString(R.string.user_sign_out_success))
                            }
                        }
                    }).onShow(childFragmentManager.beginTransaction())
                }
            }
        }

        while (isActive) {
            select {
                mUserModel.user.onReceive {
                    when(it) {
                        is UserInfoUiState.UserInfo -> {
//                            mBinding.logoutBtn.visible()
//                            mBinding.avtar.loadImgUrl(it.userInfo?.avatarUrl)
//                            mBinding.nickName.text = "${it.userInfo?.nickName}"
                        }
                        is UserInfoUiState.OrderCount -> {
//                            mOrderMenuAdapter.submitList(arrayListOf(
//                                OrderMenuInfoVo(ORDER_TYPE_PENDING,R.string.order_status_pending,R.mipmap.ic_order_pending,it.count.payment),
//                                OrderMenuInfoVo(ORDER_TYPE_SHIPMENT,R.string.order_status_shipment,R.mipmap.ic_order_shipment),
//                                OrderMenuInfoVo(ORDER_TYPE_RECEIVING,R.string.order_status_receiving,R.mipmap.ic_order_receiving,it.count.received),
//                                OrderMenuInfoVo(ORDER_TYPE_AFTER_SALES,R.string.order_status_after_sales,R.mipmap.ic_order_after_sales)
//                            ))
                        }
                        is UserInfoUiState.UserMenu -> {
//                            mMenuAdapter.submitList(it.menus)
                        }
                        is UserInfoUiState.UserUpdate -> {
                            onCenterToast(it.msg ?: getString(R.string.update_success))
                        }
                        else -> {}
                    }
                }
            }
        }

    }

    override fun onStart() {
        super.onStart()
        if(isLogin()) mUserModel.getUserInfo()
    }

}